//
//  NormalSplicingTemplateViewController.m
//  TSImageFilterDemo
//
//  Created by ciyouzen on 2017/2/25.
//  Copyright © 2017年 dvlproad. All rights reserved.
//

#import "NormalSplicingTemplateViewController.h"
#import <Masonry/Masonry.h>
#import "CQTemplateContainerView.h"
#import "GLStoryboardSelectView.h"

#import <CJBaseUIKit/UIImage+CJCreate.h>
#import "TSImageResultViewController.h"

@interface NormalSplicingTemplateViewController () <CQTemplateContainerViewDelegate, GLStoryboardSelectViewDelegate> {
    
}
@property (nonatomic, strong) CQTemplateContainerView *templateContentView;          /**< 模板上的所有图片的展示视图 */
@property (nonatomic, strong) GLStoryboardSelectView *templateStoryboardView;   /**< 拼接的样式选择 */

@property (nonatomic, strong, readonly) NSArray<UIImage *> *assets;    /**< 要处理的图片 */

@end

@implementation NormalSplicingTemplateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupViews];
    
    NSArray<UIImage *> *images = @[[UIImage imageNamed:@"bmp1.bmp"],
                                   [UIImage imageNamed:@"bmp2.bmp"],
                                   [UIImage imageNamed:@"image1.jpg"],
                                   [UIImage imageNamed:@"image2.jpg"],
                                   [UIImage imageNamed:@"0.png"],
                                ];
    _assets = images;
    
    
    [self initToolbarViewWithCount:self.assets.count];

    [self.templateContentView updateImagesByDataModels:images styleIndex:1];
}

#pragma mark - setupViews
- (void)setupViews {
    self.view.backgroundColor = [UIColor blackColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"裁剪", nil) style:UIBarButtonItemStylePlain target:self action:@selector(clipAndShowResultImage)];
    
    CQTemplateContainerView *templateContentView = [[CQTemplateContainerView alloc] initWithFrame:CGRectZero initAnimate:CQTemplateInitAnimateFirst];
    templateContentView.delegateMove = self;
    [self.view addSubview:templateContentView];
    [templateContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view).mas_offset(20);
        make.top.mas_equalTo(self.mas_topLayoutGuide).mas_equalTo(20);
        make.bottom.mas_equalTo(self.mas_bottomLayoutGuide).mas_offset(-100);
    }];
    self.templateContentView = templateContentView;
}


- (void)initToolbarViewWithCount:(NSInteger)picCount {
    CGFloat storyboardViewWidth = self.view.frame.size.width;
    CGFloat storyboardViewHeight = 50;
    GLStoryboardSelectView *templateStoryboardView = [[GLStoryboardSelectView alloc] initWithFrame:CGRectMake(0, 0, storyboardViewWidth, storyboardViewHeight) picCount:picCount];
    [templateStoryboardView setBackgroundColor:[[UIColor redColor] colorWithAlphaComponent:0.6]];
    templateStoryboardView.delegateSelect = self;
    [self.view addSubview:templateStoryboardView];
    [templateStoryboardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view).mas_offset(20);
        make.height.mas_equalTo(storyboardViewHeight);
        make.bottom.mas_equalTo(self.mas_bottomLayoutGuide).mas_offset(-0);
    }];
    self.templateStoryboardView = templateStoryboardView;
}




#pragma mark - GLStoryboardSelectViewDelegate：分镜的选择
/**
 *  分镜的选择
 *
 *  @param sender
 */
- (void)didSelectedStoryboardPicCount:(NSInteger)picCount styleIndex:(NSInteger)styleIndex {
    [self.templateContentView updateStyleIndex:styleIndex];
}


/// 裁剪并显示结果
- (void)clipAndShowResultImage {
    UIImage *image = [UIImage cj_imageWithView:self.templateContentView opaque:NO];
    UIViewController *viewController = [[TSImageResultViewController alloc] initWithResultImage:image];
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
